package com.xxxx.game;

import java.util.Vector;

import com.example.gametestproject.ResourceManager;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.RectF;
import android.util.Log;

public class FrameAnimation extends AnimationBasic implements ICanvasDrawing {

	protected Vector<Integer> mFrames;
	protected int mCurFrmIdx;
	protected boolean mRepeat;
	private int mWidth;
	private int mHeight;
	private RectF mDrawRect = null;
	
	public FrameAnimation(Vector<Integer> iFrames)
	{
		this.mFrames = new Vector<Integer>(iFrames);
		this.mCurFrmIdx = 0;
		this.mRepeat = false;
		if (this.mFrames.size() != 0)
		{
			this.mWidth = ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)).getWidth();
			this.mHeight = ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)).getHeight();
		}	
	}
	
	public FrameAnimation(Vector<Integer> iFrames, RectF idrawRect)
	{
		this.mFrames = new Vector<Integer>(iFrames);
		this.mCurFrmIdx = 0;
		this.mRepeat = false;
		if (this.mFrames.size() != 0)
		{
			this.mWidth = ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)).getWidth();
			this.mHeight = ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)).getHeight();
			this.setDrawRect(idrawRect);
		}	
	}
	
	public FrameAnimation(Vector<Integer> iFrames, int iCurFrmIdx, boolean iRepeat, RectF idrawRect)
	{
		this.mFrames = new Vector<Integer>(iFrames);
		this.mCurFrmIdx = iCurFrmIdx;
		this.mRepeat = iRepeat;
		if (this.mFrames.size() != 0)
		{
			this.mWidth = ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)).getWidth();
			this.mHeight = ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)).getHeight();
			this.setDrawRect(idrawRect);

		}
	}
	public FrameAnimation(Vector<Integer> iFrames, int iCurFrmIdx, boolean iRepeat)
	{
		this.mFrames = new Vector<Integer>(iFrames);
		this.mCurFrmIdx = iCurFrmIdx;
		this.mRepeat = iRepeat;
		if (this.mFrames.size() != 0)
		{
			this.mWidth = ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)).getWidth();
			this.mHeight = ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)).getHeight();
		}
	}
	
	public void setRepeat(boolean iRepeat)
	{
		this.mRepeat = iRepeat;
	}
	
	@Override
	public boolean paint(Canvas canvas, ISprite sprite)
	{
		if(this.mDrawRect == null)//by default, we will 
		{
			PointF sCenter = sprite.getCenter();
			RectF drawRect = new RectF(sCenter.x - this.mWidth / 2, sCenter.y - this.mHeight / 2,
					sCenter.x + this.mWidth / 2, sCenter.y + this.mHeight / 2);
			canvas.drawBitmap(ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)), null, drawRect, sprite.getPaint());
		}
		else
		{
			canvas.drawBitmap(ResourceManager.getInstance().getBitmap(this.mFrames.get(this.mCurFrmIdx)), null, this.mDrawRect, new Paint());
		}
		return true;
	}

	public void setDrawRect(RectF idrawRect)
	{
		this.mDrawRect = idrawRect;
		PointF rCenter = new PointF(this.mDrawRect.left + this.mDrawRect.width() / 2, this.mDrawRect.top + this.mDrawRect.height() / 2);
		this.mDrawRect.left = (rCenter.x - this.mWidth ) ;
		this.mDrawRect.right = (rCenter.x + this.mWidth );
		this.mDrawRect.bottom = (rCenter.y + this.mHeight );
		this.mDrawRect.top = (rCenter.y - this.mHeight );
	}
	
	@Override
	public String getName()
	{
		return "FrameAnimation";
	}

	@Override
	public boolean update(ISprite sprite)
	{
		if (this.mFrames.size() != 0)
		{
			if (this.mRepeat)
			{
				this.mCurFrmIdx = (++ this.mCurFrmIdx) % this.mFrames.size();
				return true;
			}
			if(this.mCurFrmIdx >= this.mFrames.size() - 1)
				return false;
			this.mCurFrmIdx ++;
		}
		return true;
	}

	@Override
	public boolean isMoving()
	{
		return false;
	}

}
